﻿using System;
using System.Linq;
using GiveAQuiz.Data;

namespace GiveAQuiz.Web.Helpers
{
  /// <summary>
  /// Extension methods for the Entity Framework domain objects.
  /// </summary>
  public static class DomainExtensions
  {
    /// <summary>
    /// Can we end a given Quiz?
    /// </summary>
    /// <param name="quiz">The Quiz class to extend.</param>
    /// <returns>true if the quiz is active and hasn't ended; otherwise false</returns>
    public static bool CanEnd(this Quiz quiz)
    {
      var today = DateTime.Now;

      bool isEndable = today > quiz.StartDate && today < quiz.EndDate && !quiz.HasEnded;

      return isEndable;
    }

    /// <summary>
    /// Can we edit a given Quiz Definition?
    /// </summary>
    /// <param name="quizDefinition">The QuizDefinition class to extend.</param>
    /// <returns>true if the quiz doesn't have any results; otherwise false</returns>
    public static bool CanEdit(this QuizDefinition quizDefinition)
    {
      var quizzes = quizDefinition.Quizzes;
      if ( quizzes != null )
      {
        var quizResults = quizzes.SelectMany(q => q.QuizResults);
        return !(quizResults != null && quizResults.Count() > 0);
      }
      return true;
    }

    /// <summary>
    /// Can we clear the results for a given Quiz?
    /// </summary>
    /// <param name="quiz">The Quiz class to extend.</param>
    /// <returns>true if the quiz has any results; otherwise false</returns>
    public static bool CanClearResults(this Quiz quiz)
    {
      var quizResults = quiz.QuizResults;
      return quizResults != null && quizResults.Count > 0;
    }

    /// <summary>
    /// Can we restart a given Quiz?
    /// </summary>
    /// <param name="quiz">The Quiz class to extend.</param>
    /// <returns>true if the quiz has ended and it doesn't have
    /// any results; otherwise false</returns>
    public static bool CanRestart(this Quiz quiz)
    {
      var quizResults = quiz.QuizResults;
      // to restart, must have no results and the quiz must have ended
      return (quizResults == null || quizResults.Count == 0) && quiz.HasEnded;
    }

    /// <summary>
    /// Gets the number of correct answer for a given QuizResult.
    /// </summary>
    /// <param name="qr">The QuizResult.</param>
    /// <returns>The number of correct results.</returns>
    public static int CorrectAnswers(this QuizResult qr)
    {
      return
        qr.QuizAnswers
          .Where(qa => String.CompareOrdinal(qa.Result, "Correct") == 0).Count();
    }

    /// <summary>
    /// Gets the number of inconclusive answer for a given QuizResult.
    /// </summary>
    /// <param name="qr">The QuizResult.</param>
    /// <returns>The number of inconclusive results.</returns>
    public static int InconclusiveAnswers(this QuizResult qr)
    {
      return
        qr.QuizAnswers
          .Where(qa => String.CompareOrdinal(qa.Result, "Undetermined") == 0).Count();
    }
    
  }
}
